from django.shortcuts import render

# 后来导入的模块:
from django.http import HttpResponse,Http404
from .models import Board


def first(request):
    # return HttpResponse('This is the first page I learned about django.')

    """
    视图函数:

    # 将Board类的name字段(属性)的所有数据存到boards变量中.
    boards = Board.objects.all() 
    boards_names = list()  # 转换为列表.并赋值给新变量.

    # 循环遍历boards变量,将name字段(属性)的数据追加到新变量(boards_names)中.
    for board in boards:
        boards_names.append(board.name)

    # 将name字段(属性)的数据换行,并赋值给新变量
    response_html = '<br>'.join(boards_names)

    # 返回给用户请求的页面
    return HttpResponse(response_html)
    """

    """
    # 模板引擎设置后,更新first视图:
    boards = Board.objects.all()
    return render(request,'./boards/html/first1.html',{'boards': boards})
    """

    # 模板引擎设置后,改进的html模板为表格:
    boards = Board.objects.all()
    return render(request,'./boards/html/first2.html',{'boards': boards})

'''
def board_topics(request,pk):
    try:
        board = Board.objects.get(pk=pk)
    except Board.DoesNotExist:
        raise Http404
    return render(request,'./boards/html/topics.html',{'board': board})
'''

'''
Django有一个快捷方式去得到一个对象,或者返回一个不存在的对象404
现在重写board_topics函数.
'''
from django.shortcuts import render,get_object_or_404
from .models import Board
def board_topics(request,pk):
    board = get_object_or_404(Board,pk=pk)
    return render(request,'./boards/html/topics.html',{'board': board})
    